<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Plotfav extends Model
{
    //
    protected $table = 'dd_plotfav';
    protected $primaryKey = 'id';
    public $timestamps = false;
    protected $guarded = array();
    
    
    public static function addPlotfav(\Illuminate\Http\Request $request) {
        $data = $request->all();
        $res = static::create($data);
        if ($res) {
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 1,
                'info'      => '添加成功'
            ]);
        } else {
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 0,
                'info'      => '添加失败'
            ]);
        }
    }
    
    /**
     * 产品
     * @return type
     */
    public function strProduct() {
       if ($this->ptype == 1)
            return $this->productname."(新装产品)";
        else 
            return $this->productname."(续费产品)";
    }
    /**
     * 格式化输出
     * @return type
     */
    public function strFav() {
        return number_format($this->plotfav, 2);
    }
    /**
     * 格式化输出
     * @return type
     */
    public function strInit() {
        return number_format($this->plotinit, 2);
    }
    /**
     * 优惠小区
     */
    public function strPlot() {
        $plot = Plot::find($this->plotid);
        if ($plot) {
            return $plot->name;
        } else {
            return "未知小区";
        }
    }
    
    public function strFavProduct() {
        $product = Product::find($this->productid);
        if ($product) {
            return $product->name."(".$product->strPtype().")";
        } else {
            return "未知产品";
        }
    }
    
}
